package com.hzhetun.example.controller.crm;

import com.hzhetun.example.pojo.CrmCustomer;
import com.hzhetun.example.pojo.SystemUser;
import com.hzhetun.example.service.CrmCustomerService;
import com.hzhetun.example.utils.RestfulResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author chenbingkun
 * @date 2018-12-23
 */
@Controller
@RequestMapping(value = "crm")
public class CrmCustomerController {
    @Autowired
    private CrmCustomerService customerService;

    /**
     *查询方法
     */
    @RequestMapping(value = "/query",method = RequestMethod.GET)
    public String query(ModelMap map){
//        List<SystemRole> systemRoles=systemRoleService.findRoleSelectList();
//        map.put("systemRoles",systemRoles);
        return "/crm/crm_customer_list";
    }

    @RequestMapping(value = "/data",method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> data(CrmCustomer crmCustomer, @RequestParam(value = "page") int page, @RequestParam(value = "limit") int limit){
        List<CrmCustomer> customers= customerService.findCrmCustomerList(crmCustomer,page,limit);
        Long count= customerService.count(crmCustomer);
        Map<String,Object> map=new HashMap<String,Object>(4);
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data",customers);
        return map;
    }

    @RequestMapping(value = "/addOrUpdate",method = RequestMethod.POST)
    @ResponseBody
    public RestfulResult addOrUpdate(CrmCustomer crmCustomer, HttpSession httpSession){
        /*SystemUser user= systemUserService.findByCondition(systemUser);
        if(systemUser.getId()==null){
            if(user!=null){
                return RestfulResult.warning(-10,"用户名已存在");
            }
        }
        else{
            if(user!=null && user.getId().compareTo(systemUser.getId())!=0){
                return RestfulResult.warning(-10,"用户名已存在");
            }
        }

        if(roles.length==0){
            return RestfulResult.warning(-20,"请至少选择一个角色");
        }*/

        String msg="";
        SystemUser user=(SystemUser)httpSession.getAttribute("user");
        if(crmCustomer.getId()==null){
            msg="保存成功";
        }
        else{
            msg="修改成功";
        }
        try {
            customerService.saveOrUpdate(crmCustomer,user.getUsername());
        }catch (Exception e){
            e.printStackTrace();
            return RestfulResult.error500(e.getMessage());
        }
        return RestfulResult.success(msg);
    }

    @RequestMapping(value = "/findOne")
    @ResponseBody
    public RestfulResult findOne(@RequestParam(value = "id") int id){
        CrmCustomer crmCustomer=customerService.findOne(id);
        if(crmCustomer==null){
            return RestfulResult.warning(-10,"无对应的客户信息");
        }

        Map<String,Object> map=new HashMap<String,Object>(2);
        map.put("crmCustomer",crmCustomer);
        return new RestfulResult(200,map);
    }

    @RequestMapping(value = "/del")
    @ResponseBody
    public RestfulResult del(@RequestParam(value = "ids") String ids){
        String[] id=ids.split(",");
        for(String s:id){
            int result= customerService.del(Integer.parseInt(s));
        }
        return RestfulResult.success("删除成功");
    }
}
